package com.wang.project.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.project.annotation.AuthCheck;
import com.wang.project.common.*;
import com.wang.project.constant.UserConstant;
import com.wang.project.exception.BusinessException;
import com.wang.project.model.dto.interfaceInfo.InterfaceInfoAddRequest;
import com.wang.project.model.dto.interfaceInfo.InterfaceInfoInvokeRequest;
import com.wang.project.model.dto.interfaceInfo.InterfaceInfoQueryRequest;
import com.wang.project.model.dto.interfaceInfo.InterfaceInfoUpdateRequest;
import com.wang.project.model.entity.InterfaceInfo;
import com.wang.project.model.vo.interfaceInfo.InterfaceInfoVO;
import com.wang.project.model.vo.interfaceInfo.InterfaceTotalInfoVO;
import com.wang.project.service.InterfaceInfoService;
import com.wang.project.service.InvokeTestInterfaceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 接口管理
 * 供管理员进行操作接口
 * 用户添加接口只能通过提交pr的方式进行上传接口道sdk中
 * 如果用户操作接口的关闭等，只能操作自己创建的接口
 */
@RestController
@RequestMapping("/interfaceInfo")
@Slf4j
public class InterfaceInfoController {

    @Resource
    private InterfaceInfoService interfaceInfoService;

    @Resource
    private InvokeTestInterfaceService invokeTestInterfaceService;


    /**
     * 创建接口
     * todo 新增接口可以去对接外部系统，让别人的接口也可以放在此平台上面，外部系统不需要做校验，只需要外部系统有这个接口服务就可以了
     * todo 一些校验在我们平台服务上去做，节省别人校验接口，保证接口的安全。减少外部系统开发接口的开发成本
     * 如果是管理员就直接发布接口，如果是用户自己创建接口，就进行审核区域，待审核
     *
     * @param interfaceInfoAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> addInterfaceInfo(@RequestBody InterfaceInfoAddRequest interfaceInfoAddRequest, HttpServletRequest request) {
        Boolean result = interfaceInfoService.addInterfaceInfo(interfaceInfoAddRequest, request);
        return ResultUtils.success(result);
    }

    /**
     * 删除接口
     * 暂时不支持用户删除自己创建的接口，但是用户可以禁用自己的接口
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteInterfaceInfo(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        Boolean result = interfaceInfoService.deleteInterfaceInfo(deleteRequest, request);
        return ResultUtils.success(result);
    }

    /**
     * 更新接口
     *  todo 用户自己创建的接口，用户也可以进行更新
     *
     * @param interfaceInfoUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateInterfaceInfo(@RequestBody InterfaceInfoUpdateRequest interfaceInfoUpdateRequest,
                                                     HttpServletRequest request) {
        if (interfaceInfoUpdateRequest == null || interfaceInfoUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean result = interfaceInfoService.updateInterfaceInfo(interfaceInfoUpdateRequest, request);
        return ResultUtils.success(result);
    }

    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<InterfaceInfo> getInterfaceInfoById(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        InterfaceInfo interfaceInfo = interfaceInfoService.getById(id);
        return ResultUtils.success(interfaceInfo);
    }

    /**
     * 获取列表（仅管理员可使用）
     *
     * @param interfaceInfoQueryRequest
     * @return
     */
    @GetMapping("/list")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<List<InterfaceInfo>> listInterfaceInfo(InterfaceInfoQueryRequest interfaceInfoQueryRequest) {
        List<InterfaceInfo> interfaceInfoList = interfaceInfoService.listInterfaceInfo(interfaceInfoQueryRequest);
        return ResultUtils.success(interfaceInfoList);
    }

    /**
     * 返回接口相关的所有数据 包括积分 审核等等。。
     */
    @GetMapping("/interfaceTotalList")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<List<InterfaceTotalInfoVO>> interfaceTotalList(HttpServletRequest request) {
        // todo
        return ResultUtils.success(new ArrayList<>());
    }


    /**
     * 用户获取自己申请到的接口，和自己创建的接口信息list
     *
     * @param interfaceInfoQueryRequest
     * @param request
     * @return
     */
    @GetMapping("/user/list")
    public BaseResponse<List<InterfaceInfoVO>> getUserInterfaceInfoList(InterfaceInfoQueryRequest interfaceInfoQueryRequest, HttpServletRequest request) {
        List<InterfaceInfoVO> interfaceInfoVOList = interfaceInfoService.getUserInterfaceInfoList(interfaceInfoQueryRequest, request);
        return ResultUtils.success(interfaceInfoVOList);
    }


    /**
     * 分页获取列表
     *
     * @param interfaceInfoQueryRequest
     * @param request
     * @return
     */
    @GetMapping("/list/page")
    public BaseResponse<Page<InterfaceInfo>> listInterfaceInfoByPage(InterfaceInfoQueryRequest interfaceInfoQueryRequest, HttpServletRequest request) {
        if (interfaceInfoQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<InterfaceInfo> interfaceInfoPage = interfaceInfoService.listInterfaceInfoByPage(interfaceInfoQueryRequest, request);
        return ResultUtils.success(interfaceInfoPage);
    }

    /**
     * 上线接口
     *
     * @param idRequest
     * @param request
     * @return
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) //仅管理员可调用
    @PostMapping("/online")
    public BaseResponse<Boolean> onlineInterfaceInfo(@RequestBody idRequest idRequest,
                                                     HttpServletRequest request) {
        Boolean result = interfaceInfoService.onlineInterfaceInfo(idRequest, request);
        return ResultUtils.success(result);
    }

    /**
     * 下线接口
     *
     * @param idRequest
     * @param request
     * @return
     */
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) //仅管理员可调用
    @PostMapping("/offline")
    public BaseResponse<Boolean> offlineInterfaceInfo(@RequestBody idRequest idRequest, HttpServletRequest request) {
        Boolean result = interfaceInfoService.offlineInterfaceInfo(idRequest, request);
        return ResultUtils.success(result);
    }


    /**
     * 接口测试
     *
     * @param interfaceInfoInvokeRequest
     * @param request
     * @return
     */
    @PostMapping("/invoke")
    public BaseResponse<Object> invokeInterfaceInfo(@RequestBody InterfaceInfoInvokeRequest interfaceInfoInvokeRequest,
                                                    HttpServletRequest request) {
        Object result = invokeTestInterfaceService.invokeInterfaceInfo(interfaceInfoInvokeRequest, request);
        return ResultUtils.success((String) result);
    }
}
